<?php


class Verit{
    private $width;
    private $height;
    private $bgcolor;
    private $fonts;
    private $ttfsrc;

    /**
     * Verit constructor.
     * @param int $height
     * @param int $width
     * @param array $arrcolor
     * @param int $fonts
     * @param string $ttfsrc
     */
    public function __construct($height=150,$width=300,$arrcolor=array(153,217,214),$fonts=4,$ttfsrc='./font.ttf')
    {
        $this->height=$height;
        $this->width=$width;
        $this->bgcolor=$arrcolor;
        $this->fonts=$fonts;
        $this->ttfsrc=$ttfsrc;

    }

    public function show(){  //验证码运行方法
        $this->run();

    }
    protected function run(){   //验证码运行核心代码
        header("Content-type: image/jpeg");
        $width=$this->width;
        $height=$this->height;
        $bgcolor=$this->bgcolor;
        $src = imageCreateTrueColor($width,$height);
        $bgcolor=imageColorAllocate($src,$bgcolor[0],$bgcolor[1],$bgcolor[2]);
        imageFill($src,0,0,$bgcolor);
        $this->txt($src);
        $this->line($src);
        $this->pix($src);
        $this->img($src);

    }

    protected function pix($src){  //验证码像素点方法
        for($i=0;$i<300;$i++){
            imagesetpixel($src,mt_rand(10,280),mt_rand(5,140),mt_rand(0,255));
        }

    }

    protected function line($src){ //画行方法
        for($i=0;$i<5;$i++){
            imageline($src,mt_rand(30,50),mt_rand(20,120),mt_rand(220,280),mt_rand(20,120),mt_rand(0,255));
        }

    }

    protected function txt($src){  //字体方法
        $fonts=$this->fonts;
        $ttfsrc=$this->ttfsrc;
        for($i=0;$i<$fonts;$i++){
            $arr=array('a','b','c','d','e','f','g','7','1','3','2','4','9','6','5','8');
            $res=mt_rand(0,count($arr)-1);
            $code=$arr[$res];
            $width=$i*60;
            $width+=18;
            imagettftext($src,mt_rand(40,65),mt_rand(-30,30),$width,mt_rand(80,110),mt_rand(0,255),$ttfsrc,$code);
        }

    }
    protected function img($src){
        imagejpeg($src);
    }


}